package com.service.business.conversion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.service.business.base.dto.LoginUserSession;
import com.service.business.base.service.CoinsPriceService;
import com.service.business.base.service.ConfigService;
import com.service.business.conversion.controller.rep.ConversionCoinsConfigRep;
import com.service.business.conversion.controller.rep.ConversionConfigRep;
import com.service.business.conversion.service.ConvertCoinsRecordService;
import com.service.business.conversion.service.ConvertConfigService;
import com.service.business.member.service.MemberCacheService;
import com.service.business.member.service.MemberGradeConfigService;
import com.service.business.member.service.MemberWalletService;
import com.service.business.support.SessionUtil;
import com.service.dao.constants.ConfigConstants;
import com.service.dao.enums.CoinsEnum;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.enums.WalletEnums;
import com.service.dao.model.ConvertConfig;
import com.service.dao.model.MemberGradeConfig;
import com.service.dao.po.MemberCache;
import com.service.utility.common.MathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.service.dao.mapper.ConversionConfigMapper;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;


@Service("conversionConfigService")
public class ConvertConfigServiceImpl extends ServiceImpl<ConversionConfigMapper, ConvertConfig> implements ConvertConfigService {

    @Autowired
    private SessionUtil sessionUtil;
    @Autowired
    private ConfigService configService;
    @Autowired
    private CoinsPriceService coinsPriceService;
    @Autowired
    private MemberCacheService memberCacheService;
    @Autowired
    private MemberGradeConfigService memberGradeConfigService;
    @Autowired
    private ConvertCoinsRecordService convertCoinsRecordService;

    @Override
    public ConversionCoinsConfigRep config(CoinsEnum coinsEnum) {
        LoginUserSession session = sessionUtil.getUser();
        Map<String, BigDecimal> coinPrice = coinsPriceService.getStandardPrice2Map(Boolean.FALSE);
        ConversionConfigRep configRep = getConversionCoinsConfig(coinsEnum, coinPrice);
        AtomicReference<BigDecimal> totalNum = new AtomicReference<>(BigDecimal.ZERO);
        configRep.getSourceCoins().forEach(item -> {
            totalNum.set(totalNum.get().add(item.getCoinsNum().multiply(item.getCoinsPrice())));
        });
        configRep.setCoinsNum(MathUtil.div(totalNum.get(), coinPrice.get(configRep.getCoinsName())));
        MemberCache memberCache = memberCacheService.getMemberCache(session.getUserId());
        MemberGradeConfig memberGradeConfig = memberGradeConfigService.getById(memberCache.getMemberGrade());
//        Map<String, String> config = configService.getConfig2MapByGroup(ConfigConstants.Group.conversion.getCode());

        BigDecimal conversionNum = convertCoinsRecordService.getTotalConversionNum(session.getUserId(), coinsEnum, LocalDate.now());
        BigDecimal allConversionNum = convertCoinsRecordService.getTotalConversionNum(null, coinsEnum, LocalDate.now());
//        WalletEnums.Type walletType = WalletEnums.Type.valueOf(config.get(ConfigConstants.CONVERSION_CONTRACT_ADDRESS_TYPE));

        return ConversionCoinsConfigRep.builder().build();
//        return ConversionCoinsConfigRep.builder()
//                .config(configRep)
//                .conversionNumDaily(MathUtil.parseBigDecimal(config.get(ConfigConstants.CONVERSION_NUM_DAILY)))
//                .conversionNumMultiple(MathUtil.parseBigDecimal(config.get(ConfigConstants.CONVERSION_NUM_MULTIPLE)))
//                .memberConversionNumDaily(memberGradeConfig.getConversionNumDaily())
//                .memberConversionNum(conversionNum)
//                .allConversionNum(allConversionNum)
//                .minNum(MathUtil.parseBigDecimal(config.get(ConfigConstants.CONVERSION_NUM_MIN)))
//                .walletType(walletType.getName())
//                .build();
    }

    private ConversionConfigRep getConversionCoinsConfig(CoinsEnum coinsEnum, Map<String, BigDecimal> coinPrice) {
        ConversionConfigRep result = new ConversionConfigRep();
        result.setSourceCoins(new ArrayList<>());
        List<ConvertConfig> configList = listByTargetCoin(coinsEnum);

        configList.forEach(item -> {
            result.setCoinsName(item.getTargetCoinsName());
            result.setCoinsPrice(coinPrice.get(item.getTargetCoinsName()));
            result.getSourceCoins().add(ConversionConfigRep.builder()
                    .coinsName(item.getSourceCoinsName())
                    .coinsPrice(coinPrice.get(item.getSourceCoinsName()))
                    .coinsNum(item.getSourceNum()).build());
        });

        return result;
    }

    public List<ConvertConfig> listByTargetCoin(CoinsEnum coinsEnum) {
        return listByTargetCoinId(coinsEnum.getCode());
    }

    private List<ConvertConfig> listByTargetCoinId(Integer coinsId) {
        return list(new QueryWrapper<ConvertConfig>().lambda()
                .eq(ConvertConfig::getTargetCoinsId, coinsId)
                .eq(ConvertConfig::getConfigStatus, CommonStatusEnums.ACTIVE.getCode()));
    }


}
